package com.ferrlo.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.onlineframework.commons.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ferrlo.common.Constant;
import com.ferrlo.model.User;
import com.ferrlo.service.IUserManager;
import com.ferrlo.servlet.SampleImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;

@Controller
public class LoginController {

	@Resource(name = "userManager")
	private IUserManager userManager;

	@RequestMapping(value = "/login")
	public String login(Map<String, Object> map, HttpServletRequest request) {

		ImageCaptchaService service = SampleImageCaptchaService.getInstance();

		String username = request.getParameter("username");
		String pwd = MD5Util.getMD5(request.getParameter("pwd"));
		String captcha = request.getParameter("captcha");

		User user = findUserByNickName(username);

		if (service.validateResponseForID(request.getSession(true).getId(),
				captcha)) {
			if (user != null) {
				if (pwd.equals(user.getUPwd())) {
					System.out.println(" ===== SUCCESS ==");
					request.setAttribute("user", user);
					request.getSession().setAttribute(Constant.KEY_LOGIN_USER,
							user.getUName());
					request.getSession().setAttribute(
							Constant.KEY_LOGIN_ISLOGINED, "true");
				} else {
					System.out.println("===== FAIL ====");
				}
			}
		} else {
			System.out.println("NO");
		}

		return "redirect:index";
	}

	@RequestMapping(value = "/login.json")
	@ResponseBody
	public Map<String, Object> login_JSON(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();

		ImageCaptchaService service = SampleImageCaptchaService.getInstance();
		
		String callback = request.getParameter("callback");
		System.out.println("============================");
		System.out.println(callback);
		System.out.println("============================");
		

		String username = request.getParameter("username");
		String pwd = MD5Util.getMD5(request.getParameter("pwd"));
		String captcha = request.getParameter("captcha");

		User user = findUserByNickName(username);
		if (service.validateResponseForID(request.getSession(true).getId(),
				captcha)) {
			if (user != null) {
				if (pwd.equals(user.getUPwd())) {
					System.out.println(" ===== SUCCESS ==");
					request.getSession().setAttribute(Constant.KEY_LOGIN_USER,
							user.getUName());
					request.getSession().setAttribute(
							Constant.KEY_LOGIN_ISLOGINED, "in");
					map.put("msg", "success");
				} else {
					System.out.println("===== FAIL ====");
					map.put("msg", "fail");
				}
			}
		} else {
			System.out.println("NO");
			map.put("msg", "fail");
		}
		 

		return map;
	}

	private User findUserByNickName(String nickName) {

		String dqQuery = "UserManager.findUserByUNickName.query";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("nickName", nickName);
		List<User> users = userManager.findListByDynamicQuery(dqQuery, param);
		if (users.size() == 0) {
			return null;
		} else {
			return users.get(0);
		}
	}

}
